import { MPJCheckboxGroup } from './../../components/widgets/checkbox-group';
import Initialization from './initialization'
import Welcome from './welcome'
import ConnectLDC from './connectLDC'
import CheckEnv from './checkEnv'
import RemoteLogin from './remoteLogin'


export class InitializationService {
    static current = new InitializationService();

    route
    onComplete
    constructor() {
        this.route = {
            path: 'initialization',
            name: 'initialization',
            component: Initialization,
            children: [ 
                { path: "", name: 'welcome', url: "initialization/", component: Welcome },
                { path: "welcome", name: 'welcome', url: "initialization/welcome", component: Welcome },
                { path: "connectLDC", name: 'connectLDC', url: "initialization/connectLDC", component: ConnectLDC },
                { path: "checkEnv", name: 'checkEnv', url: "initialization/checkEnv", component: CheckEnv },
                { path: "remoteLogin", name: 'remoteLogin', url: "initialization/remoteLogin", component: RemoteLogin }
            ]
        }

        //外部的事件
        this.onComplete = (data: {
            store: any;
            LDCConfig: any;
            user: any;
            adsAbout: any;
            token: string;
        }) => { }
    }
    components = [MPJCheckboxGroup] 

    forRoot() {
        function traverse(data) {
            let arr = [];
            arr.push(data.component);
            for (let key in data) {
                if (Array.isArray(data[key])) {
                    data[key].forEach(v => {
                        arr = arr.concat(traverse(v))
                    })
                }
            }
            return arr;
        }
        let components = traverse(this.route);
        return components.concat(this.components);
    }

    getPathByComponentName(name) {
        if (this.route.name == name) {
            return this.route.path;
        }
        return this.route.children.find(x => x.name == name).url
    }

    registerChildrenRoute(route){
        this.route.children && this.route.children.push(route);
    }
}

